<?php
global $connect;

if (isset($_POST['iqteprod']))
{
	$q = "UPDATE emplacements SET quantite='".$_POST['iqteprod']."' WHERE nmagasin = '".$_GET['show_mag_id']."' AND nproduit = '".$_POST['post_id']."';";
	mysql_query($q) or die(mysql_error());
}

function create_form($id, $idmag)
{
	$query = "SELECT cat.nomcategorie AS 'CATEGORIE',prd.nproduit AS NPRODUIT,  prd.marque AS 'MARQUE',em.cellule, em.quantite, em.maxi  FROM produits prd
		LEFT JOIN categories cat ON (cat.ncategorie = prd.ncategorie)
		LEFT JOIN emplacements em ON (prd.nproduit = em.nproduit)
			WHERE em.nmagasin = '$idmag' AND prd.nproduit = '$id' ORDER BY prd.marque;";
	$r = mysql_query($query) or die(mysql_error());
	$data = mysql_fetch_array($r);
	echo "<form action='index.php?page=magasins&show_mag_id=$idmag&action=edit_qte_prd&id=$id' method='post' name='edit' id='form-edit-qte'>";
	echo "<tr > <td align='center' width='120px'><a href='index.php?page=magasins&show_mag_id=".$_GET['show_mag_id']."&action=edit&edit_prod_id=".$data['NPRODUIT']."'>".$data['MARQUE']." </a></td>";
	echo "<td align='center' width='120px'>".$data['CATEGORIE']." </td>";
	echo "<td align='center' width='120px'>".$data['cellule']." </td>";
	echo "<td align='center' width='120px'><input type='text' name='iqteprod' value=".$data['quantite']." style='text-align:right;padding-right:5px;'> </td>";
	echo "<td align='center' width='120px'>".$data['maxi']."</td>";
	echo "<td align='center' width='120px'><input type='submit' value='Save'/><input type=hidden name='post_id' value='".$id."'></td></tr>";
	echo "</form>";
}

$query = "SELECT cat.nomcategorie AS 'CATEGORIE',prd.nproduit AS NPRODUIT,  prd.marque AS 'MARQUE',em.cellule, em.quantite, em.maxi  FROM produits prd
    LEFT JOIN categories cat ON (cat.ncategorie = prd.ncategorie)
    LEFT JOIN emplacements em ON (prd.nproduit = em.nproduit)
  WHERE em.nmagasin = '".$_GET['show_mag_id']."' ORDER BY prd.marque;";

$mag = mysql_query($query) or die(mysql_error());

if (isset($_GET['edit_prod_id']))
	include ('pages/edit_prod.php');
else if (isset($_GET['del_prod_id']))
	include ('pages/del_prod.php');
else
{
?>
<center><h1><a href='index.php?page=produits&action=add&idmag=<?php echo $_GET['show_mag_id']?>'><img src='img/list-add.png'  width='20px' height='20px'/> Ajouter un produit </a></h1></center> <br /> <br />
<h1>Liste des produits en Magasin</h1>
	<!--<form method="post" action="index.php?page=magasins&action=create_cmd">-->
	<table frame="border" border="1" cellspacing="0">
		<thead>
			<tr> <th>Marque</th> <th>Categories</th> <th>Cellule</th> <th>Quantite</th> <th>Maximum</th> <th>Actions</th></tr>		</thead>
		<tbody>
		<?php
			while ($data = mysql_fetch_array($mag))
			{
				if (isset($_GET['action']) and $_GET['action'] == 'edit_qte_prd' and isset($_GET['id']) and $_GET['id'] == $data['NPRODUIT'] and !isset($_POST['iqteprod']))
					create_form($_GET['id'], $_GET['show_mag_id']);
				else
				{
				$nbr = $data['maxi'] - $data['quantite'];
				echo "<tr > <td align='center' width='120px'><a href='index.php?page=magasins&show_mag_id=".$_GET['show_mag_id']."&action=edit&edit_prod_id=".$data['NPRODUIT']."'>".$data['MARQUE']." </a></td>";
				echo "<td align='center' width='120px'>".$data['CATEGORIE']." </td>";
				echo "<td align='center' width='120px'>".$data['cellule']." </td>";
				echo "<td align='center' width='120px'>".$data['quantite']." </td>";
				echo "<td align='center' width='120px'>".$data['maxi']."</td>";
				echo "<td align='center' width='120px'><a title='Modifier la quantite presente' href='index.php?page=magasins&show_mag_id=".$_GET['show_mag_id']."&action=edit_qte_prd&id=".$data['NPRODUIT']."'><img src='img/basket.png' width='20px' height='20px'/></a>";
				echo "<a href='index.php?page=magasins&show_mag_id=".$_GET['show_mag_id']."&del_prod_id=".$data['NPRODUIT']."'><img src='img/Delete.png' width='20px' height='20px'/></a></td>";
				}
			}
		?>
			
		</tbody>
	</table><!--
	<input type="hidden" name="idmag" value="<?php //echo $_GET['show_mag_id'];?>" />
	</form>-->
	<br />
	<center><h3><a href="index.php?page=magasins&action=livraison&id_mag=<?php echo $_GET['show_mag_id'] ?>">Livraison en cours sur ce magasin</a></h3></center>
	<center><h3><a href="index.php?page=magasins&action=create_cmd&show_mag_id=<?php echo $_GET['show_mag_id'] ?>">Passer une commande</a></h3></center>
	<center><h3><a href="index.php?page=magasins&action=transfert&id_mag=<?php echo $_GET['show_mag_id'] ?>">Approvisioner ce Magasin</a></h3></center>
	<center><h3><a href="index.php?page=magasins&action=import_ventes&id_mag=<?php echo $_GET['show_mag_id'] ?>">Import CSV de vente pour ce magasin</a></h3></center><br />
	<h1><center><a href="index.php?page=magasins"><img src="img/jamembo-back.png" width="30px" height="30px"/> Retour a la liste des magasins</a></center></h1>
	<?php }?>